using System;
using System.Collections.Generic;
using System.Text;
using DTO;
using System.Data;
using System.Data.SqlClient;

namespace DAO
{
	/// <summary> 
	///Author: daiduong19051986@gmail.com
	/// <summary>
    public class csreportgroupsDataAccess : Connection
    {
		#region Local Variable
        private string _strSPInsertName = "dbo.[proccsreportgroups_add]";
        private string _strSPUpdateName = "dbo.[proccsreportgroups_update]";
        private string _strSPDeleteName = "dbo.[proccsreportgroups_delete]";
        private string _strSPGetName = "dbo.[proccsreportgroups_get]";
        private string _strSPGetAllName = "dbo.[proccsreportgroups_getall]";
		private string _strSPGetPages = "dbo.[proccsreportgroups_getpaged]";
		private string _strSPIsExist = "dbo.[proccsreportgroups_isexist]";
        private string _strTableName = "[csreportgroups]";
		private string _strSPGetTransferOutName = "dbo.[proccsreportgroups_gettransferout]";
        private string _strSPGetByRoleID = "[dbo].[proccsreportgroups_getbyDA]";
		#endregion Local Variable
		
		#region Method
        public csreportgroupsInfo Get(
        String DB,
        String ReportGroup,
		ref string sErr)
        {
			csreportgroupsInfo objEntr = new csreportgroupsInfo();
            InitConnect();
			InitSPCommand(_strSPGetName);              
            AddParameter(csreportgroupsInfo.Field.DB.ToString(), DB);
            AddParameter(csreportgroupsInfo.Field.ReportGroup.ToString(), ReportGroup);
            
            DataTable list = new DataTable();
            try
            {
                list = executeSelect();
            }
            catch (Exception ex)
            {
                sErr = ex.Message;
            }
            finally {closeConnection();}  
            
            if (list.Rows.Count > 0)
                objEntr = (csreportgroupsInfo)GetDataFromDataRow(list, 0);
            //if (dr != null) list = CBO.FillCollection(dr, ref list);
            if (sErr != "") ErrorLog.SetLog(sErr);
            return objEntr;
        }

        protected override object GetDataFromDataRow(DataTable dt, int i)
        {
            csreportgroupsInfo result = new csreportgroupsInfo();
            result.DB = (dt.Rows[i][csreportgroupsInfo.Field.DB.ToString()] == DBNull.Value ? "" : Convert.ToString(dt.Rows[i][csreportgroupsInfo.Field.DB.ToString()]));
            result.ReportGroup = (dt.Rows[i][csreportgroupsInfo.Field.ReportGroup.ToString()] == DBNull.Value ? "" : Convert.ToString(dt.Rows[i][csreportgroupsInfo.Field.ReportGroup.ToString()]));
            result.Name = (dt.Rows[i][csreportgroupsInfo.Field.Name.ToString()] == DBNull.Value ? "" : Convert.ToString(dt.Rows[i][csreportgroupsInfo.Field.Name.ToString()]));
            result.DAGid = (dt.Rows[i][csreportgroupsInfo.Field.DAGid.ToString()] == DBNull.Value ? "" : Convert.ToString(dt.Rows[i][csreportgroupsInfo.Field.DAGid.ToString()]));
            result.Createdby = (dt.Rows[i][csreportgroupsInfo.Field.Createdby.ToString()] == DBNull.Value ? "" : Convert.ToString(dt.Rows[i][csreportgroupsInfo.Field.Createdby.ToString()]));
            result.Insertpointoftime = (dt.Rows[i][csreportgroupsInfo.Field.Insertpointoftime.ToString()] == DBNull.Value ? DateTime.Now : Convert.ToDateTime(dt.Rows[i][csreportgroupsInfo.Field.Insertpointoftime.ToString()]));
            result.Updatedby = (dt.Rows[i][csreportgroupsInfo.Field.Updatedby.ToString()] == DBNull.Value ? "" : Convert.ToString(dt.Rows[i][csreportgroupsInfo.Field.Updatedby.ToString()]));
            result.Lastupdate = (dt.Rows[i][csreportgroupsInfo.Field.Lastupdate.ToString()] == DBNull.Value ? DateTime.Now : Convert.ToDateTime(dt.Rows[i][csreportgroupsInfo.Field.Lastupdate.ToString()]));
           
            return result;
        }

        public DataTable GetAll(
        ref string sErr)
        {
            InitConnect();
            InitSPCommand(_strSPGetAllName);
            DataTable list = new DataTable();
            try
            {
                list = executeSelect();
            }
            catch (Exception ex)
            {
                sErr = ex.Message;
            }
            finally {closeConnection();}


            if (sErr != "") ErrorLog.SetLog(sErr);
            return list;
        }
		/// <summary>
        /// Return 1: Table is exist Identity Field
        /// Return 0: Table is not exist Identity Field
        /// Return -1: Erro
        /// </summary>
        /// <param name="tableName"></param>
        public Int32 Add(csreportgroupsInfo objEntr, ref string sErr)
        {
            int ret = -1;
            openConnection();
            InitSPCommand(_strSPInsertName);
            AddParameter(csreportgroupsInfo.Field.DB.ToString(), objEntr.DB);
            AddParameter(csreportgroupsInfo.Field.ReportGroup.ToString(), objEntr.ReportGroup);
            AddParameter(csreportgroupsInfo.Field.Name.ToString(), objEntr.Name);
            AddParameter(csreportgroupsInfo.Field.DAGid.ToString(), objEntr.DAGid);
            AddParameter(csreportgroupsInfo.Field.Createdby.ToString(), objEntr.Createdby);
            AddParameter(csreportgroupsInfo.Field.Insertpointoftime.ToString(), objEntr.Insertpointoftime);
            AddParameter(csreportgroupsInfo.Field.Updatedby.ToString(), objEntr.Updatedby);
            AddParameter(csreportgroupsInfo.Field.Lastupdate.ToString(), objEntr.Lastupdate);
          
            try
            {
                //command.ExecuteNonQuery();
                object tmp = executeScalar();
                if(tmp != null && tmp != DBNull.Value)
					ret = Convert.ToInt32(tmp);
				else 
					ret=0;
            }
            catch (Exception ex)
            {
                sErr = ex.Message;
            }
            finally {closeConnection();}
            if (sErr != "") ErrorLog.SetLog(sErr);
			
            return ret;
        }

        public string Update(csreportgroupsInfo objEntr)
        {
            openConnection();
            InitSPCommand(_strSPUpdateName);
            AddParameter(csreportgroupsInfo.Field.DB.ToString(), objEntr.DB);
            AddParameter(csreportgroupsInfo.Field.ReportGroup.ToString(), objEntr.ReportGroup);
            AddParameter(csreportgroupsInfo.Field.Name.ToString(), objEntr.Name);
            AddParameter(csreportgroupsInfo.Field.DAGid.ToString(), objEntr.DAGid);
            AddParameter(csreportgroupsInfo.Field.Createdby.ToString(), objEntr.Createdby);
            AddParameter(csreportgroupsInfo.Field.Insertpointoftime.ToString(), objEntr.Insertpointoftime);
            AddParameter(csreportgroupsInfo.Field.Updatedby.ToString(), objEntr.Updatedby);
            AddParameter(csreportgroupsInfo.Field.Lastupdate.ToString(), objEntr.Lastupdate);
               
            string sErr = "";
            try
            {
                executeNonQuery();
            }
            catch (Exception ex)
            {
                sErr = ex.Message;
            }
            finally {closeConnection();}
            if (sErr != "") ErrorLog.SetLog(sErr);
            return sErr;
        }

        public string Delete(
        String DB,
        String ReportGroup
		)
        {
            openConnection();
            InitSPCommand(_strSPDeleteName);
            AddParameter(csreportgroupsInfo.Field.DB.ToString(), DB);
            AddParameter(csreportgroupsInfo.Field.ReportGroup.ToString(), ReportGroup);
              
            string sErr = "";
            try
            {
                executeNonQuery();
            }
            catch (Exception ex)
            {
                sErr = ex.Message;
            }
            finally {closeConnection();}
            if (sErr != "") ErrorLog.SetLog(sErr);
            return sErr;
        }   
		
		
        public Boolean IsExist(
        String DB,
        String ReportGroup
		)
        {
            InitConnect();
            InitSPCommand(_strSPIsExist);
            AddParameter(csreportgroupsInfo.Field.DB.ToString(), DB);
            AddParameter(csreportgroupsInfo.Field.ReportGroup.ToString(), ReportGroup);
              
            string sErr = "";
            DataTable list = new DataTable();
            try
            {
                list = executeSelect();
            }
            catch (Exception ex)
            {
                sErr = ex.Message;
            }
            finally {closeConnection();}
            if (sErr != "") ErrorLog.SetLog(sErr);
            if(list.Rows.Count==1)
				return true;
            return false;
        }
		
		private string CreateWhereClause(csreportgroupsInfo obj)
        {
            String result = "";

            return result;
        }
        
        public DataTable Search(string columnName, string columnValue, string condition, string tableName, ref string sErr)
        {
            string query = "select * from " + _strTableName + " where " + columnName + " " + condition + " " + columnValue;
            DataTable list = new DataTable();
            openConnection();
            try
            {
                list = executeSelectQuery(query);
            }
            catch (Exception ex)
            {
                sErr = ex.Message;
            }
            finally {closeConnection();}
            //if (dr != null) list = CBO.FillCollection(dr, ref list);
            //    if (sErr != "") ErrorLog.SetLog(sErr);
            return list;
        }
		public DataTable GetTransferOut(string dtb, object from, object to, ref string sErr)
        {
            InitConnect();
            InitSPCommand(_strSPGetTransferOutName);
			AddParameter("DB", dtb);
			AddParameter("FROM", from);
			AddParameter("TO", to);
            DataTable list = new DataTable();
            try
            {
                list = executeSelect();
            }
            catch (Exception ex)
            {
                sErr = ex.Message;
            }
            finally {closeConnection();}


            if (sErr != "") ErrorLog.SetLog(sErr);
            return list;
        }
		#endregion Method


        public DataTable GetGroupbyDA(string DB, string ROLE_ID, ref string sErr)
        {

            InitConnect();
            InitSPCommand(_strSPGetByRoleID);
            AddParameter(csreportgroupsInfo.Field.DB.ToString(), DB);
            AddParameter("ROLE_ID", ROLE_ID);  
            DataTable list = new DataTable();
            try
            {
                list = executeSelect();
            }
            catch (Exception ex)
            {
                sErr = ex.Message;
            }
            finally {closeConnection();}

            //if (list.Rows.Count > 0)
            //    objEntr = (csreportgroupsInfo)GetDataFromDataRow(list, 0);
            //if (dr != null) list = CBO.FillCollection(dr, ref list);
            if (sErr != "") ErrorLog.SetLog(sErr);
            return list;
        }
    }
}
